package com.sl.hx.shop.ui.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.FlashSaleList;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.ui.home.adapter.FlashSaleAdapter;
import com.sl.hx.shop.ui.home.contacts.FlashSaleContacts;
import com.sl.hx.shop.ui.home.presenter.FlashSalePresenter;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 限时抢购fragment
 */

public class FlashSaleFragment extends BaseFragment<FlashSaleContacts.view,FlashSalePresenter> implements FlashSaleContacts.view {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;

    private FlashSaleAdapter flashSaleAdapter;
    private List<FlashSaleList> flashSaleList = new ArrayList<>();

    private String startTime;//请求开始时间
    private String endTime;//请求结束时间
    private Boolean isRefresh = true;

    private int page = 1;
    private int num = 10;

    @Override
    protected FlashSalePresenter createPresenter() {
        return new FlashSalePresenter(mContext);
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_flashsale;
    }

    @Override
    protected void initView(View mView) {
        if (getArguments() != null) {
            startTime = getArguments().getString("flashSaleStart");
            endTime = getArguments().getString("flashSaleEnd");
        }
        refresh();
        initRecycle();
        mPresenter.getFlashSale(startTime,endTime,page,num);
    }

    private void initRecycle() {
        flashSaleAdapter = new FlashSaleAdapter(flashSaleList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(flashSaleAdapter);
        flashSaleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                isRefresh=false;
                mPresenter.getFlashSale(startTime,endTime,page,num);
            }
        },mRecyclerView);
        flashSaleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
                intent.putExtra("num_iid",flashSaleAdapter.getItem(position).getNum_iid());
                startActivity(intent);
            }
        });
    }

    private void refresh() {
        mRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.main_color));
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh=true;
                page=1;
                mPresenter.getFlashSale(startTime,endTime,page,num);
            }
        });

    }

    @Override
    public void ShowFlashSale(List<FlashSaleList> flashSaleLists) {
        if (isRefresh) {
            flashSaleAdapter.setNewData(flashSaleLists);
        }else {
            if (flashSaleLists != null && flashSaleLists.size() > 0) {
                flashSaleAdapter.addData(flashSaleLists);
            }
        }
        //关闭加载更多
        if (flashSaleLists.size() < num) {
            flashSaleAdapter.loadMoreEnd();
        } else {
            flashSaleAdapter.loadMoreComplete();
        }
        //关闭刷新
        if (mRefresh.isRefreshing()){
            mRefresh.setRefreshing(false);
        }
    }
}
